#include "GBTrackBar.h"
#include "MeshManager.h"
#include <windows.h>
#include <glee.h>
#include <gl.h>

using namespace MeshComponent;

GBTrackBar::GBTrackBar() : m_min(0.0f), m_max(0.0f), m_trackerSize(4.0f, 16.0f), 
						   m_trackerPosX(0.0f)
{

}

GBTrackBar::~GBTrackBar()
{ 

}

void GBTrackBar::Render()
{
	if(m_meshID == -1) return;

	// Binding, VBOs
	MeshManager* pMM = MeshManager::GetInstance();
	unsigned vertexID = pMM->GetMeshBufferID(m_meshID, MESH_BUFFER_TYPE_VERTEX);
	unsigned texCoordID = pMM->GetMeshBufferID(m_meshID, MESH_BUFFER_TYPE_TEXCOORD);
	unsigned indexID = pMM->GetMeshBufferID(m_meshID, MESH_BUFFER_TYPE_INDEX);

	if(!vertexID && !indexID) return;

	glLineWidth(Max(1.0f, m_lineSize.Y()));
	glColor4fv(m_color.v);

	glPushMatrix();
	{
		// Draw a line for the tracker to sit on
		// Limit is so you cannot rotate the track bar
		glTranslatef(m_local.wAxis().X(), m_local.wAxis().Y(), 0.0f);

		glBegin(GL_LINES);
		{
			glVertex2f(0.0f, 0.0f);	// Begin (-x)
			glVertex2f(m_lineSize.X(), 0.0f);	// End (+x)
		}
		glEnd();

		glLineWidth(1.0f);

		// Draw the tracker in the track bar's world space
		// wAxis.x -|-------- scale.X()
	}
	glPopMatrix();

	glColor4fv(m_trackerColor.v);
	glPushMatrix();
	{
		// Now translate it to get the real position of the tracker
		glTranslatef(m_trackerPosX, m_local.wAxis().Y(), 0.0f);

		glScalef(m_trackerSize.X(), m_trackerSize.Y(), 1.0f);

		glBindBuffer(GL_ARRAY_BUFFER, vertexID);
		glVertexPointer(3, GL_FLOAT, 0, 0);

		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID);
		glIndexPointer(GL_UNSIGNED_SHORT, 0, 0);

		if(texCoordID != 0)
		{
			glBindBuffer(GL_ARRAY_BUFFER, texCoordID);
			glTexCoordPointer(2, GL_FLOAT, 0, 0);
		}

		unsigned numTriangles = pMM->GetTriangleCount(m_meshID);
		glDrawRangeElements(GL_TRIANGLES, 0, (numTriangles * 3) - 1, numTriangles * 3, GL_UNSIGNED_SHORT, 0);

		glBindBuffer(GL_ARRAY_BUFFER, 0);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	}
	glPopMatrix();
}

void GBTrackBar::Update(const float fTimeStep)
{ 
	GBWidget::Update(fTimeStep);

	if(this->m_meshID == -1)
		this->m_meshID = MeshManager::GetInstance()->LoadQuad(NULL);

	// Update the tracker by interpolating between the beginning and ending vec
	// Make sure a min is a MIN and a max is a MAX
	m_min = Min(m_min, m_max);
	m_max = Max(m_min, m_max);

	vec2 lineBegin = vec2(m_local.wAxis().X(), 0.0f);
	vec2 lineEnd = vec2(m_local.wAxis().X() + m_lineSize.X(), 0.0f);

	Clamp(lineBegin.X(), m_trackerPosX, lineEnd.X());
}

void GBTrackBar::Copy(const GBWidget* pRef)
{ 
	GBWidget::Copy(pRef);

	// Specific member vars for a track bar
	GBTrackBar* pTrackBar = (GBTrackBar*)pRef;

	this->m_lineSize = pTrackBar->m_lineSize;
	this->m_trackerColor = pTrackBar->m_trackerColor;
	this->m_trackerSize = pTrackBar->m_trackerSize;
	this->m_min = pTrackBar->m_min;
	this->m_max = pTrackBar->m_max;
	this->m_trackerPosX = pTrackBar->m_trackerPosX;
}